package com.star.wellconnect.sqlite;

import java.util.Date;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

import com.star.wellconnect.signuplogin.SignupLoginActivity;

public class AppPreferences {
	public static final String SQL_DATE = "yyyy-MM-dd";
	public static final String MILITARY_TIME = "HH:mm";
	public static final String AM_PM_TIME = "hh:mm a";
	public static final String DOW_DATE = "E MM/dd/yyyy";
	public static final String DOW_MIY_DATE = "EEEE MMM dd, yyyy";
	public static final String DOW_SMIY_DATE = "E MMM dd, yyyy";
	public static final String IS_LOGGED_IN = "IsLoggedIn";
	public static final String USERNAME = "Username";
	private static SharedPreferences sp = null;
	private static DatabaseHelper db = null;

	private static void initialize(Context context){
		if(db == null)
			db = new DatabaseHelper(context);
		if(sp == null)
			sp = PreferenceManager.getDefaultSharedPreferences(context);
	}
	
	public static boolean usernameTaken(Context context, String username){
		initialize(context);
		return db.usernameTaken(username);
	}
	
	public static long addPatient(Context context, Patient patient, String password){
		initialize(context);
		return db.addPatient(patient, password);
	}
	
	public static void logout(Context context){
		initialize(context);
		Editor editor = sp.edit();

        editor.putBoolean(AppPreferences.IS_LOGGED_IN, false);
        editor.putString(AppPreferences.USERNAME, null);
        editor.commit();
		
		Intent intent = new Intent(context, SignupLoginActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);
	}
	
	public static Patient getPatient(Context context){
		initialize(context);
		return db.getPatient(sp.getString(USERNAME, ""));
	}
	
	public static String getPatientPassword(Context context){
		initialize(context);
		return db.getPatientPassword(sp.getString(USERNAME, ""));
	}
	
	public static int updatePatient(Context context, Patient patient){
		initialize(context);
		return db.updatePatient(patient);
	}
	
	public static int updatePatientPassword(Context context, String password){
		initialize(context);
		return db.updatePatientPassword(sp.getString(USERNAME, ""), password);
	}
	
	public static boolean isLoggedIn(Context context){
		initialize(context);
		return sp.getBoolean(IS_LOGGED_IN, false);
	}
	
	public static long addAppointment(Context context, Appointment appt, String username){
		initialize(context);
		return db.addAppointment(appt, username);
	}
	
	public static List<Appointment> getAppointments(Context context, String username){
		initialize(context);
		Date now = new Date();
		return db.getAllAppointments(username, now);
	}
	
	public static Appointment getAppointment(Context context, String username, int apptId){
		initialize(context);
		return db.getAppointment(username, apptId);
	}
}
